export declare interface node {
    value: any;
    className:string | undefined | null;
    next: node | undefined | null;
    left: node | undefined | null;
    [key: string]: any;
}
export default class Swiper{
    private readonly nodeList;
    private readonly key:any;

    /** 构造方法 传入识别的索引key 以及需要循环的data[]*/
    constructor(data: any,key:string) {
        this.key = key;
        this.nodeList = data.map((ele:any) => this.createNode(ele)) // 生成空节点
        this.nodeList.forEach((node:node,index:number) => {
            node.left = index === 0 ? this.nodeList[this.nodeList.length - 1] : this.nodeList[index - 1]; // 设置左
            node.next = index === this.nodeList.length - 1 ? this.nodeList[0] : this.nodeList[index + 1]; // 设置右
        })
    }
    createNode = (item:node):node => ({ // 创建节点
        ...item,
        className:item?.className || null,
        value: item[this.key],
        next: null,
        left: null,
    })


    /** 获取根，后续需要传入使用，保证vue数据响应式 */
    getRoot = ():node | undefined =>  this.nodeList[0];

    /** 获取class样式 （传入根结点和当前v-for循环出得元素） */
    getClass = (item:any,root:any):any => this.nodeList.reduce((total: any, ele: node) => {
        const obj = {[<string>ele.className]: root[this.key] === item[this.key]}
        root = root.next
        return Object.assign(total, obj)
    }, {})
    /** 选取元素置为主元素， （传入根结点和当前v-for循环出得元素）*/
    selectMain = (item:any,root:any) => {
        while(root.value?.index !== item.index){
            root.value = root.value?.next;
        }
    }

}
